package org.dromara.event.controller;

import java.io.IOException;
import java.util.List;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.event.domain.vo.EventDataVo;
import org.dromara.event.domain.bo.EventDataBo;
import org.dromara.event.service.IEventDataService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动
 *
 * @author 吴俊杰
 * @date 2025-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/data")
public class EventDataController extends BaseController {

    private final IEventDataService eventDataService;

    /**
     * 查询活动列表
     */
    @SaCheckPermission("event:data:list")
    @GetMapping("/list")
    public TableDataInfo<EventDataVo> list(EventDataBo bo, PageQuery pageQuery) {
        return eventDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出活动列表
     */
    @SaCheckPermission("event:data:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventDataBo bo, HttpServletResponse response) {
        List<EventDataVo> list = eventDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "活动", EventDataVo.class, response);
    }

    /**
     * 获取活动详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("event:data:query")
    @GetMapping("/{id}")
    public R<EventDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eventDataService.queryById(id));
    }


    /**
     * 上传图片
     */
    @SaCheckPermission("event:data:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public R<String> upload(@RequestPart("file") MultipartFile file) throws IOException {
        return R.ok(eventDataService.uploadImage(file));
    }

    /**
     * 新增活动
     */
    @SaCheckPermission("event:data:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventDataBo bo) throws IOException {
        return toAjax(eventDataService.insertByBo(bo));
    }

    /**
     * 修改活动
     */
    @SaCheckPermission("event:data:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventDataBo bo) {
        return toAjax(eventDataService.updateByBo(bo));
    }

    /**
     * 删除活动
     *
     * @param ids 主键串
     */
    @SaCheckPermission("event:data:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eventDataService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 审核活动
     *
     *
     */
    @SaCheckPermission("event:data:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public R<Void> audit(@Validated(EditGroup.class) @RequestBody EventDataBo bo) {
        return toAjax(eventDataService.audit(bo));
    }

    /**
     * 活动广场
     */
    @SaCheckPermission("event:data:list")
    @GetMapping("/square")
    public TableDataInfo<EventDataVo> square(EventDataBo bo, PageQuery pageQuery) {
        bo.setAuditStatus("1");
        bo.setStatus("1");
        TableDataInfo<EventDataVo> tableDataInfo = eventDataService.queryPageList(bo, pageQuery);
        return tableDataInfo;
    }

    /**
     * 我参与的活动
     */
    @SaCheckPermission("event:data:list")
    @GetMapping("/my")
    public TableDataInfo<EventDataVo> my(EventDataBo bo, PageQuery pageQuery) {
        TableDataInfo<EventDataVo> tableDataInfo = eventDataService.my(bo, pageQuery);
        return tableDataInfo;
    }

    /**
     * 随机抽取四张活动图片的活动id作为轮播图
     */
    @GetMapping("/random")
    public R<List<EventDataVo>> random() {
        return R.ok(eventDataService.random());
    }
}
